<template>
  <div class="app-container">
    <el-form :inline="true" size="small" label-width="100">
      <el-form-item>
        <el-button size="small" class="btn-add" @click="handleAdd">添加</el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="resourceCategoryTable" height="100%" :data="list" style="width: 100%" v-loading="listLoading" border>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="名称" minWidth="120" align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="创建时间" width="160" align="center">
          <template #default="scope">{{ formatDateTime(scope.row.createTime) }}</template>
        </el-table-column>
        <el-table-column label="排序" align="center">
          <template #default="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="handleUpdate(scope.row)"> 编辑 </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="添加分类" v-model="dialogVisible" width="40%">
      <el-form :model="resourceCategory" ref="resourceCategoryForm" label-width="auto" size="small">
        <el-form-item label="名称：">
          <el-input v-model="resourceCategory.name"></el-input>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input v-model="resourceCategory.sort"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="handleDialogConfirm" size="small">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { listAllCate, createResourceCategory, updateResourceCategory, deleteResourceCategory } from '@/api/resourceCategory'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { ResourceCategory, ResourceCategoryParams } from '@/models/resourceCategory'
import { formatDateTime } from '@/utils/format'

defineOptions({ name: 'ResourceCategoryList' })

const list = ref<ResourceCategory[]>([])
const listLoading = ref(false)
const dialogVisible = ref(false)
const resourceCategory = ref<ResourceCategoryParams>({})
getList()
function handleAdd() {
  dialogVisible.value = true
  resourceCategory.value = {}
}
function handleUpdate(row: ResourceCategoryParams) {
  dialogVisible.value = true
  resourceCategory.value = row
}
async function handleDelete(row: ResourceCategoryParams) {
  await ElMessageBox.confirm('是否要删除该分类?', '提示', { type: 'warning' })
  await deleteResourceCategory(row.id as number)
  ElMessage.success('删除成功！')
  getList()
}
async function handleDialogConfirm() {
  if (resourceCategory.value.id) {
    await updateResourceCategory(resourceCategory.value.id, resourceCategory.value)
    ElMessage.success('添加成功！')
  } else {
    await createResourceCategory(resourceCategory.value)
    ElMessage.success('添加成功！')
  }
  dialogVisible.value = false
  getList()
}
async function getList() {
  listLoading.value = true
  const response = await listAllCate()
  listLoading.value = false
  list.value = response.data.data
}
</script>
<style></style>
